package com.snap.vseries;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;
import android.view.WindowManager;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.snap.vseries.logger.ExpLogAdapter;
import com.snap.vseries.logger.LoggerUtil;
import com.snap.vseries.logger.UserLogAdapter;
import com.snap.vseries.ui.widget.Layer;

import org.apache.commons.math3.analysis.function.Exp;
import org.litepal.LitePal;
import org.litepal.parser.LitePalAttr;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class MoleApplication extends Application {

    private static MoleApplication instance;
    private String experimentInfoName;

    public static MoleApplication get() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Logger.i("***********设备启动***********");
        instance = this;
        initRGB();
        initLog();
        initUncaughtException();
        //初始化数据库
        LitePal.initialize(this);
        //修改数据库存储目录
        LitePalAttr.getInstance().setStorage(AppConfig.DATABASE_DIRECTORY);
        //删除日志,只保留七天的日志
        deleteUserLog();
    }


    private void initRGB() {
        //透明度
        int alpha = 0;
        int r = 6;
        int g = 6;
        int b = 0;
        //明暗对比度
        int contrast = 0;
        Layer rView = new Layer(this);
        Layer gView = new Layer(this);
        Layer bView = new Layer(this);

        rView.setColor(r, 255, 0, 0);
        gView.setColor(g, 0, 255, 0);
        bView.setColor(b, 0, 0, 255);


        Layer alphaView = new Layer(this);
        Layer contrastView = new Layer(this);
        alphaView.setColor(alpha, 0, 0, 0);
        contrastView.setColor(contrast, 100, 100, 100);

        WindowManager.LayoutParams params = new WindowManager.LayoutParams(-1, -1, 2006, 280, -2);
        WindowManager manager = (WindowManager) getSystemService("window");
        manager.addView(bView, params);
        manager.addView(gView, params);
        manager.addView(rView, params);
//        manager.addView(contrastView, params);
//        manager.addView(alphaView, params);

    }

    private void deleteUserLog() {

    }

    /**
     * 初始化Logger日志工具
     */
    private void initLog() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return BuildConfig.DEBUG;
            }
        });
        Logger.addLogAdapter(new UserLogAdapter());
        Logger.addLogAdapter(new ExpLogAdapter());
    }

    /**
     * init uncaught exception
     * 捕获并处理在一个线程对象中抛出的未检测异常,以避免程序终止
     */
    private void initUncaughtException() {
        Thread.setDefaultUncaughtExceptionHandler((t, e) -> Logger.e("捕捉到异常：" + LoggerUtil.printExceptionInfo(e)));
    }

    public static String printExceptionInfo(Throwable e) {
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        e.printStackTrace(printWriter);
        printWriter.close();
        return writer.toString();
    }

}
